package base.datastructure.tree;


import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lizx
 * @ClassName:
 * @Description:
 * @date 2020/9/15 19:43
 */
public class TreeBuilder {
    List<? extends NodeHelper> nodes = new ArrayList<>();

    public TreeBuilder(List<? extends NodeHelper> nodes) {
        super();
        this.nodes = nodes;
    }

    /**
     * 构建树形结构
     *
     * @return
     */
    public List<NodeHelper> buildTree() {
        List<NodeHelper> treeNodes = new ArrayList<>();
        List<NodeHelper> rootNodes = getRootNodes();
        for (NodeHelper rootNode : rootNodes) {
            buildChildNodes(rootNode);
            treeNodes.add(rootNode);
        }
        return treeNodes;
    }

    /**
     * 递归子节点
     *
     * @param node
     */
    public void buildChildNodes(NodeHelper node) {
        List<NodeHelper> children = getChildNodes(node);
        if (!children.isEmpty()) {
            for (NodeHelper child : children) {
                buildChildNodes(child);
            }
            node.setChild(children);
        }
    }

    /**
     * 获取父节点下所有的子节点
     *
     * @param pnode
     * @return
     */
    public List<NodeHelper> getChildNodes(NodeHelper pnode) {
        List<NodeHelper> childNodes = new ArrayList<>();
        for (NodeHelper n : nodes) {
            if (pnode.getId().equals(n.getParentId())) {
                childNodes.add(n);
            }
        }
        return childNodes;
    }

    /**
     * 判断是否为根节点
     *
     * @param node
     * @return
     */
    public boolean rootNode(NodeHelper node) {
        boolean isRootNode = true;
        for (NodeHelper n : nodes) {
            if (node.getParentId().equals(n.getId())) {
                isRootNode = false;
                break;
            }
        }
        return isRootNode;
    }

    /**
     * 获取集合中所有的根节点
     *
     * @return
     */
    public List<NodeHelper> getRootNodes() {
        List<NodeHelper> rootNodes = new ArrayList<>();
        for (NodeHelper n : nodes) {
            if (rootNode(n)) {
                rootNodes.add(n);
            }
        }
        return rootNodes;
    }


    public static class NodeTestVo extends NodeHelper {

        private String text;

        private String url;

        public NodeTestVo(Integer id, Integer parentId, String text, String url) {
            super();
            this.id = id;
            this.parentId = parentId;
            this.text = text;
            this.url = url;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        @Override
        public String toString() {
            return "NodeTestVo{" +
                    "text='" + text + '\'' +
                    ", url='" + url + '\'' +
                    '}';
        }
    }

    public static void main(String[] args) {
        List<NodeTestVo> nodes = new ArrayList<>();
        NodeTestVo p1 = new NodeTestVo(1, 0, "1", "");
        NodeTestVo p6 = new NodeTestVo(2, 0, "2", "");
        NodeTestVo p7 = new NodeTestVo(201, 2, "201", "");
        NodeTestVo p2 = new NodeTestVo(101, 1, "101", "");
        NodeTestVo p3 = new NodeTestVo(102, 1, "102", "");
        NodeTestVo p4 = new NodeTestVo(10101, 101, "10101", "");
        NodeTestVo p5 = new NodeTestVo(10102, 101, "10102", "");
        nodes.add(p1);
        nodes.add(p2);
        nodes.add(p3);
        nodes.add(p4);
        nodes.add(p5);
        nodes.add(p6);
        nodes.add(p7);
        TreeBuilder treeBuilder = new TreeBuilder(nodes);
        List<NodeHelper> nodeVos = treeBuilder.buildTree();
        System.out.println(JSON.toJSONString(nodeVos));
    }

}
